
import os
import shutil
import tkinter
from tkinter import Label, Entry, Button
from PIL import Image, ImageTk

markimg = os.path.join(os.getcwd(), 'markimg')

file_list = []

def get_files():
    codeimg = os.path.join(os.getcwd(), 'codeimg')
    for root,_,files in os.walk(codeimg):
        for f in files:
            file_list.append({
                'rootpath': root,
                'filename': f
            })

class MainWindow:
    def __init__(self) -> None:
        self.app = tkinter.Tk()
        self._init_window()

    def _init_window(self):
        ww = 400
        wh = 500
        sw = self.app.winfo_screenwidth()
        sh = self.app.winfo_screenheight()
        x = (sw - ww) / 2
        y = (sh - wh) / 2
        self.app.geometry('%dx%d+%d+%d' % (ww, wh, x, y))
        # 禁止调整大小
        self.app.resizable(False, False)
        self.app.title('图片验证码-人工标记工具')

        lab1 = Label(self.app, text='验证码', name='code')
        lab1.place(x=10, y=10)

        lab2 = Label(self.app, text='标记', name='mark')
        lab2.place(x=180, y=10)

        self.__build_img()

        but = Button(
            self.app, 
            name='save',
            text='保存', width=10, height=2, command=self.__save_info)
        but.place(x=180, y=450)

        self.app.mainloop()

    def __build_img(self):
        _child = self.app.children
        for key in list(_child.keys()):
            val = _child.get(key)
            winfo_name = val.winfo_name()
            if winfo_name in ['code', 'mark', 'save']:
                continue
            val.destroy()

        for i in range(10):
            if not file_list:
                continue
            file_item = file_list.pop()
            rootpath = file_item.get('rootpath')
            filename = file_item.get('filename')
            filepath = os.path.join(rootpath, filename)
            img = Image.open(filepath)

            # 引用：添加一个Label，用来存储图片。使用PanedWindow也行。
            panel = Label(master=self.app)
            # 将原本的变量photo改为panel.photo
            panel.photo = ImageTk.PhotoImage(img)

            # 注意：参数image改为panel.photo
            lab_img = Label(self.app, image=panel.photo)
            y = (i + 1) * 40
            print(f'filepath:{filepath}, y={y}')
            lab_img.place(x=10, y=y)
            
            ent = Entry(self.app)
            ent.myinfo = file_item
            ent.place(x=180, y=y)

    def __save_info(self):
        print(len(self.app.children))
        for key,val in self.app.children.items():
            widgetName = val.widgetName
            if not widgetName == 'entry':
                continue
            myinfo = val.myinfo
            var = val.get()
            if not var:
                continue
            print(f'key:{key},myinfo:{myinfo},widgetName={widgetName},var={var}')

            rootpath = myinfo.get('rootpath')
            filename = myinfo.get('filename')
            src_path = os.path.join(rootpath, filename)

            new_name = f'{var}.jpeg'
            tar_path = os.path.join(markimg, new_name)
            shutil.move(src_path, tar_path)

        self.__build_img()



if __name__ == '__main__':
    get_files()
    MainWindow()
